﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cherry.Agile.Framework.Query;
using Cherry.Agile.ServiceModel;
using Cherry.Agile.Serialize.Json;
using System.Diagnostics;
namespace Cherry.Agile.ServiceModel
{
    internal class PaginationProxyHandler
    {
        const string _key = "__paging";

        internal static void HandlerInputs(object[] inputs)
        {
            if (inputs == null || inputs.Length == 0)
                return;
            List<Pagination> ary = new List<Pagination>();
            foreach (var item in inputs)
            {
                var p = item as Pagination;
                if (p != null)
                {
                    //AttachToContext(p);
                    //break;
                    ary.Add(p);
                }
                else
                {
                    var q = item as QueryInformation;
                    if (q != null && q.Pagination != null)
                    {
                        ary.Add(q.Pagination);
                        //AttachToContext(q.Pagination);
                        //break;
                    }
                }

            }
            if (ary.Count > 0)
                AttachToContext(ary.ToArray());
            Trace.TraceInformation("PaginationProxyHandler\tbefore handle service operation inputs ");
        }

        internal static void HandlerResults(object[] inputs)
        {
            var page = FindFromContext();
            if (page == null)
                return;
            if (inputs == null || inputs.Length == 0)
                return;
            var index = 0;
            foreach (var item in inputs)
            {
                var p = item as Pagination;
                if (p != null)
                {
                    page[index].ApplyTo(p);
                    index += 1;
                   
                }
                else
                {
                    var q = item as QueryInformation;
                    if (q != null && q.Pagination != null)
                    {
                        page[index].ApplyTo(q.Pagination);
                        index += 1;
                       
                    }
                }
                if (index >= page.Length)
                    break;
            }
            Trace.TraceInformation("PaginationProxyHandler\tafter handle service operation inputs ");
        }

        private static Pagination[] FindFromContext()
        {
            if (ApplicationContext.Current[_key] != null)
                return ApplicationContext.Current[_key].ToString().JsonDeserialize<Pagination[]>();  //return JsonConvert.DeserializeObject<Pagination[]>(ApplicationContext.Current[_key].ToString());
            return null;
        }

        private static void AttachToContext(Pagination[] values)
        {
            ApplicationContext.Current[_key] = values.JsonSerialize();// JsonConvert.SerializeObject(values);
        }


    }
}
